package cn.chuanw.db.service;

import cn.chuanw.db.dao.WHouseInfoMapper;
import cn.chuanw.db.dao.WRoomInfoMapper;
import cn.chuanw.db.domain.WHouseInfo;
import cn.chuanw.db.domain.WHouseInfoExample;
import cn.chuanw.db.domain.WRoomInfo;
import cn.chuanw.db.domain.WRoomInfoExample;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class WHouseService {
	@Resource
	private WHouseInfoMapper wHouseInfoMapper;
	@Resource
	private WRoomInfoMapper wRoomInfoMapper;

	public List<WHouseInfo> querySelective(String community, String fullName, String landlord, Integer leaseStatus,
										   String playTime, Integer playFlag, Integer page, Integer size) {
		WHouseInfoExample wHouseInfoExample = new WHouseInfoExample();
		WHouseInfoExample.Criteria criteria = wHouseInfoExample.createCriteria();
		if (!StringUtils.isEmpty(leaseStatus)) {
			criteria.andLeaseStatusEqualTo(leaseStatus);
		}
		if (!StringUtils.isEmpty(community)) {
			criteria.andCommunityLike("%" + community + "%");
		}
		if (!StringUtils.isEmpty(fullName)) {
			criteria.andFullNameLike("%" + fullName + "%");
		}
		if (!StringUtils.isEmpty(landlord)) {
			criteria.andLandlordLike("%" + landlord + "%");
		}
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		LocalDate localDate = null;
		if (!StringUtils.isEmpty(playTime)) {
			localDate = LocalDate.parse(playTime, formatter);
		}
		if (!StringUtils.isEmpty(localDate)) {
			criteria.andPlayTimeEqualTo(localDate);
		}
		if (!StringUtils.isEmpty(playFlag)) {
			criteria.andPlayFlagEqualTo(playFlag);
		}

		criteria.andDelFlagEqualTo(0);
		// this.playFlagUpdate();
		PageHelper.startPage(page, size);
		List<WHouseInfo> wHouseInfos = wHouseInfoMapper.selectByExample(wHouseInfoExample);

		return wHouseInfos;
	}

	public void addWHouseInfo(WHouseInfo wHouseInfo) {
		wHouseInfoMapper.insertSelective(wHouseInfo);
	}

	public int updateById(WHouseInfo wHouseInfo) {
		return wHouseInfoMapper.updateByPrimaryKeySelective(wHouseInfo);
	}

	public List<WHouseInfo> queryHouse() {
		WHouseInfoExample wHouseInfoExample = new WHouseInfoExample();
		WHouseInfoExample.Criteria criteria = wHouseInfoExample.createCriteria();
		criteria.andDelFlagEqualTo(0);
		return wHouseInfoMapper.selectByExample(wHouseInfoExample);
	}

	public List<WRoomInfo> queryRoomByHouseId(String houseId) {
		WRoomInfoExample example = new WRoomInfoExample();
		example.createCriteria().andHouseIdEqualTo(houseId);
		return wRoomInfoMapper.selectByExample(example);
	}

	public List<WHouseInfo> playFlagUpdate() {

		WHouseInfoExample wHouseInfoExample = new WHouseInfoExample();
		WHouseInfoExample.Criteria criteria = wHouseInfoExample.createCriteria();
		criteria.andDelFlagEqualTo(0);
		List<WHouseInfo> wHouseInfos = wHouseInfoMapper.selectByExample(wHouseInfoExample);
			int nowMonth = LocalDateTime.now().getMonthValue();
			int nowDay = LocalDateTime.now().getDayOfMonth();
			int nowYear = LocalDateTime.now().getYear();
			// 获取自公元元年至今所有月份
			int allMonth = nowYear * 12 + nowMonth;
			for (WHouseInfo wHouseInfo : wHouseInfos) {
				// 获取房屋支付方式 1押一付一 2押一付二 3押一付三 4半年付 5全款付清
				int cycle = Integer.parseInt(wHouseInfo.getCycle());
				// 判断支付时间是否为空
				if (wHouseInfo.getPlayTime() != null) {
					//欠租状态
					Period next = Period.between(wHouseInfo.getPlayTime(),LocalDate.now());
					//   int months = next.getMonths();
					 int days =  next.getDays();
					if(days>0){
						wHouseInfo.setPlayFlag(3);
						wHouseInfoMapper.updateByPrimaryKeySelective(wHouseInfo);
					}
					// 判断交租时间为当月并且当月已经过去的时间不更新
					if (wHouseInfo.getPlayTime().getMonthValue() == nowMonth
							&& wHouseInfo.getPlayTime().getDayOfMonth() >= nowDay) {

						int month = wHouseInfo.getPlayTime().getMonthValue();
						int yer = wHouseInfo.getPlayTime().getYear();

						// 获取公元元年至起租时间所有月份，
						int beginAllMonth = yer * 12 + month;
						if (allMonth >= beginAllMonth) {
							// 已租所有月份
							int pastMonth = allMonth - beginAllMonth;
							// 余出的月份，整除说明 需要交租 押一付一绝对整除表示每月交一次，每个月都显示一次，押一付而
							// 余一表示下个月要交租，押一付三，余几就是剩余几个月要交租
							int surplusMonth = pastMonth % cycle;

							// 交租状态 需要交租的改为即将交租
							if (surplusMonth == 0) {
								wHouseInfo.setPlayFlag(2);
								wHouseInfoMapper.updateByPrimaryKeySelective(wHouseInfo);
							}

						}

					}
				}

			}
		
		return wHouseInfos;

	}

}
